import axios from 'axios'
const base = '/api/isv'
import ElementUI from 'element-ui'
import $store from "@/store/index"

// 得到全局活动的列表
function getActivityList(params) {
  return new Promise((resolve) => {
    let resData = {
      success: '',
      msg: '',
      data: []
    }
    axios.get(`${base}/activity/queryAll`, params).then(res => {
      console.log('res.data', res.data);
      if (res.data.success) {
        resData.success = true;
        resData.data = res.data.result;
        // 赋值活动列表
        $store.state.activityList = resData.data;
      } else {
        ElementUI.Message.warning({
          message: res.data.errorMsg || '网络异常',
          type: 'warning'
        })
      }
      resolve(resData);
    }, err => {
      ElementUI.Message.error(`网络异常`);
      resolve(resData);
    });
  });
}

export {
  getActivityList
}

// 票务统计-票种统计
function ticketCount() {
  return new Promise((resolve) => {
    let resData = {
      success: '',
      msg: '',
      data: []
    }
    axios.post(`${base}/infoticket/type`, {
      activityId: $store.state.thisActivityId
    }).then(res => {
      if (res.data.success) {
        $store.state.showTicketCountChart = false;
        resData.success = true;
        resData.data = res.data.result;
        // 赋值票务统计-票种统计接口数据
        $store.state.ticketCount = resData.data;
        // 图表数据
        $store.state.lineChartData.xName = [];
        $store.state.lineChartData.barData = [];
        $store.state.lineChartData.showName = ["票种"];
        $store.state.lineChartData.total = $store.state.ticketCount.num;
        if ($store.state.ticketCount.list.length != 0) {
          $store.state.ticketCount.list.forEach(item => {
            $store.state.lineChartData.xName.push(item.typeName);
            $store.state.lineChartData.barData.push(item.number);
          });
        }
        $store.state.showTicketCountChart = true;
      } else {
        ElementUI.Message.warning({
          message: res.data.errorMsg || '网络异常',
          type: 'warning'
        })
      }
      resolve(resData);
    }, err => {
      ElementUI.Message.error(`网络异常`);
      resolve(resData);
    });
  });
}

export {
  ticketCount
}


// 票务统计-渠道统计
function getShareListData() {
  return new Promise((resolve) => {
    let resData = {
      success: '',
      msg: '',
      data: []
    }
    axios.post(`${base}/infoticket/shareurl`, {
      activityId: $store.state.thisActivityId
    }).then(res => {
      if (res.data.success) {
        $store.state.showShareListChart = false;
        resData.success = true;
        resData.data = res.data.result;
        $store.state.shareList = resData.data;
        // 图表数据
        $store.state.lineChartDataShareList.xName = [];
        $store.state.lineChartDataShareList.barData = [];
        $store.state.lineChartDataShareList.showName = ["渠道"];
        $store.state.lineChartDataShareList.total = $store.state.shareList.num;
        if ($store.state.shareList.list.length != 0) {
          $store.state.shareList.list.forEach(item => {
            $store.state.lineChartDataShareList.xName.push(item.name);
            $store.state.lineChartDataShareList.barData.push(item.number);
          });
        }
        $store.state.showShareListChart = true;
      } else {
        ElementUI.Message.warning({
          message: res.data.errorMsg || '网络异常',
          type: 'warning'
        })
      }
      resolve(resData);
    }, err => {
      ElementUI.Message.error(`网络异常`);
      resolve(resData);
    });
  });
}
export {
  getShareListData
}



// 之前的
export const getShareList = params => {
  return axios.post(`${base}/infoticket/shareurl`, params)
} // 渠道统计
